<script lang="ts" setup>
import type { VbenFormSchema } from '@vben/common-ui';

import { computed, onMounted, reactive, ref } from 'vue';
import { useRoute } from 'vue-router';

import { AuthenticationLogin, z } from '@vben/common-ui';
import { $t } from '@vben/locales';

import { useAuthStore } from '#/store';

defineOptions({ name: 'Login' });

const authStore = useAuthStore();
const formSchema = computed((): VbenFormSchema[] => {
  return [
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: $t('authentication.usernameTip'),
      },
      fieldName: 'username',
      label: $t('authentication.username'),
      defaultValue: 'admin',
      rules: z.string().min(1, { message: $t('authentication.usernameTip') }),
    },
    {
      component: 'VbenInputPassword',
      componentProps: {
        placeholder: $t('authentication.password'),
      },
      fieldName: 'password',
      defaultValue: '123456',
      label: $t('authentication.password'),
      rules: z.string().min(1, { message: $t('authentication.passwordTip') }),
    },
    // {
    //   component: markRaw(SliderCaptcha),
    //   fieldName: 'captcha',
    //   rules: z.boolean().refine((value) => value, {
    //     message: $t('authentication.verifyRequiredTip'),
    //   }),
    // },
  ];
});
const loginRef = ref();
const loginPropsRef = ref();
const route = useRoute();
const urlParams = new URLSearchParams(window.location.search);

const thirdAuth = reactive({
  accountId: route.query.accountId || urlParams.get('accountId'),
  platform: route.query.platform || urlParams.get('platform'),
  tenantCode: route.query.tenantCode || urlParams.get('tenantCode'),
});
onMounted(async () => {
  if (thirdAuth.accountId && thirdAuth.platform) {
    await authStore.authLogin(
      {
        tenantCode: thirdAuth.tenantCode,
        username: thirdAuth.accountId,
        password: thirdAuth.platform,
        loginType: thirdAuth.platform,
      },
      () => {
        const newUrl =
          window.location.origin +
          window.location.pathname +
          window.location.hash;
        window.history.replaceState({}, '', newUrl);
        window.location.href = newUrl;
      },
    );
  }
});

function handleSubmit(params: any, onSuccess?: () => Promise<void> | void) {
  // const giteeIntercept = import.meta.env.VITE_GITEE_INTERCEPT;
  // if (giteeIntercept === true || giteeIntercept === 'true') {
  //   thirdAuthGitee().then((ret: any) => {
  //     window.location.href = ret.authorizeUrl;
  //   });
  // } else {
  params.loginType = '1';
  authStore.authLogin(params, onSuccess);
  // }
}
</script>

<template>
  <AuthenticationLogin
    ref="loginRef"
    :form-schema="formSchema"
    :loading="authStore.loginLoading"
    :show-code-login="false"
    :show-forget-password="false"
    :show-qrcode-login="false"
    :show-register="false"
    :show-remember-me="loginPropsRef?.showRememberMe"
    :show-third-party-login="false"
    :sub-title="$t('请登录您的账户信息')"
    :title="$t('欢迎使用')"
    @submit="handleSubmit"
  />
</template>
